package com.example.javaee.controller;

import com.example.javaee.domain.Comment;
import com.example.javaee.exception.CourseAdminException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;

import com.example.javaee.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * @Description: 评论控制器
 */
@CrossOrigin
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    //添加评论
    @PostMapping("/saveComment")
    public Comment saveComment(@RequestBody Comment comment) throws CourseAdminException
    {
        return commentService.saveComment(comment);
    }

    //查询评论
    @GetMapping("/listComment")
    public List<Comment> listComment() throws CourseAdminException {
        return commentService.listComment();
    }

    @GetMapping("/findByCourseNumber/{courseNumber}")
    public List<Comment> findByCourseNumber(@PathVariable Long courseNumber) throws CourseAdminException, IOException {
        return commentService.findBycourseNumber(courseNumber);
    }

    @GetMapping("/findByUserId/{userId}")
    public List<Comment> findByUserId(@PathVariable Integer userId) throws CourseAdminException
    {
        return commentService.findByUserId(userId);
    }

    @PutMapping("/like")
    public Void addLike(Integer userId, Long commentId) throws CourseAdminException {
        commentService.addLikeCount(userId, commentId);
        return null;
    }

    @PutMapping("/dislike")
    public Void addDislike(Integer userId, Long commentId) throws CourseAdminException {
        commentService.addDislikeCount(userId, commentId);
        return null;
    }

    @PutMapping("/undoLike")
    public Void undoLike(Integer userId, Long commentId) throws CourseAdminException {
        commentService.undoLike(userId, commentId);
        return null;
    }

    @PutMapping("/undoDislike")
    public Void undoDislike(Integer userId, Long commentId) throws CourseAdminException {
        commentService.undoDislike(userId, commentId);
        return null;
    }

    @DeleteMapping("/deleteComment/{id}")
    public Void deleteComment(@PathVariable Long id) // throws CourseAdminException
    {
        commentService.deleteComment(id);
        return null;
    }

    @GetMapping("/adminFindByCourseNumber")
    public List<Comment> adminFindByCourseNumber(Long courseNumber){
        List<Comment> result = new ArrayList<>();
        result = commentService.adminFindByCourseNumber(courseNumber);
        return result;
    }

    //查询出子评论
    /*
    @PostMapping("/comment")
    public String post(Comment comment) {
        //设置头像
        comment.setAvatar(avatar);
        if (comment.getParentComment().getId() != null) {
            comment.setParentCommentId(comment.getParentComment().getId());
        }
        commentService.saveComment(comment);
        return "redirect:/comment";
    }
    */
}
